<style scoped>
.div_1{width:500px;margin: 0px auto;}
</style>

<template>
<div class="div_1">
    <Form ref="from" :model="user" :rules="ruleInline" >
        <FormItem prop="username">
            <Input type="text" v-model="user.username" placeholder="Username">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem prop="password">
            <Input type="password" v-model="user.password" placeholder="Password">
                <Icon type="ios-locked-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="login">Signin</Button>
        </FormItem>
    </Form>
</div>
</template>
<script>
export default {
    data(){
        return {
            user:{username:'admin',password:'star'},
            ruleInline:{
                username:[
                    { required: true, message: 'Please fill in the user name', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: 'Please fill in the password.', trigger: 'blur' },
                    { type: 'string', min: 4, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
                ]
            }
        }
    },methods:{
        login(){
            if(this.handleSubmit('from')){
                this.axios.post('/user/login',this.user).then(res=>{
                    console.log(res);
                    if(res.errorCode==0){
                        this.$store.dispatch('set_user',res.data);
                        var t=this.$route.query.redirect || '/';
                        this.$router.push(t);
                    }
                })
            }
        },handleSubmit(name){
            var t=true;
            this.$refs[name].validate((valid) => {
                if (valid) {
                    //this.$Message.success('Success!');
                    t=true;
                } else {
                    //this.$Message.error('Fail!');
                    t=false;
                }
            })
            return t;
        }
    }
}
</script>